<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Home\Model;
use Think\Model;
class CollectionModel extends Model{
    public $error   =   '';
    //自动完成
//    protected $_auto = array(
//        array('status', 1, self::MODEL_INSERT),
//        array('create_at', NOW_TIME, self::MODEL_INSERT),
//    );

    public function update($data){
        $map    =   array();
        $map['id']      =   $data['id']?$data['id']:0;
        $map['user_id'] =   $data['user_id']?$data['user_id']:0;
        $map['goods_id'] = $data['goods_id']?$data['goods_id']:0;
        $map = array_filter($map);
        $res    =   $this->where($map)->select();
        if($res){
            if($data = $this->create($data) && $this->where($map)->save($data)){
                return true;
            }
            $this->error = '已经添加过';
        }else{
            if($this->create($data) && $this->add($data)){
                return true;
            }
            $this->error = '新增失败';
        }
        return false;
    }

    public function getCollection($user_id){
        $map = array();
        $map['user_id'] = $user_id;
        $res = $this->where($map)->select();
        return $res;
    }
    public function del($id,$user_id=0){
        if($user_id){
            $map = array('user_id'=>$user_id);
        }else{
            if(empty($id)){
                $this->error = '请选择要删除的配置项';
            }
            $map = array('id'=>array('IN',trim(implode(',',$id),',')));
        }

        //$res = $this->where($map)->delete();
        if($this->where($map)->delete()){
            return true;
        }else{
            $this->error = '无此条数据';
            return false;
        }
    }
    public function getError(){
        return $this->error;
    }
}